import json

from cpbox.tool import http

PGYER_URL_PRE = 'https://www.pgyer.com/'
PGYER_ICON_URL_PRE = 'https://appicon.pgyer.com/image/view/app_icons/'
PGYER_UPLOAD_URL = PGYER_URL_PRE + 'apiv2/app/upload'
DING_TALK_WEB_HOOK_URL = 'https://oapi.dingtalk.com/robot/send?access_token=4fc2a0c37cd1c3e59603ae9eb10e6df0135be836b28585a6f7ef33b9d88af5c7'
FEISHU_WEB_HOOK_DEV_URL = 'https://open.feishu.cn/open-apis/bot/v2/hook/841455a0-17ef-441d-b532-7cc6cee4486e'
FEISHU_WEB_HOOK_URL = 'https://open.feishu.cn/open-apis/bot/v2/hook/deb629c1-816b-4d93-b5ca-8eefeccc9a7c'


class Market():

    def __init__(self, app):
        self.app = app

    def upload_to_market(self, markets=None):
        pass

    def _get_all_markets(self, markets=None):
        if markets is None:
            markets = 'pyger, xiaomi'
        return markets.split(',')

    def upload_to_pgyer(self, env, filepath):
        pgyer = Pgyer(env, filepath)
        pgyer.upload()


class Pgyer():
    def __init__(self, env, filepath):
        self.env = env
        self.filePath = filepath
        self.uKey = 'b5947525e88330598fddb8651d45b4d9'
        self.apiKey = 'f8c051b06d50e00f4e0f2d76bd65b1eb'
        self.buildType = 2
        self.buildPassword = 123

    def upload(self):
        files = {}
        files['file'] = open(self.filePath, 'rb')

        values = {}
        values['uKey'] = self.uKey
        values['_api_key'] = self.apiKey
        values['buildInstallType'] = self.buildType
        values['buildPassword'] = self.buildPassword

        print("market.py start upload task.")
        ret = http.post(PGYER_UPLOAD_URL, files=files, data=values)
        result = http.fetch_json(PGYER_UPLOAD_URL, ret)
        print(result)
        self._send_notification(result['data'])

    def _send_notification(self, result_data):
        app_name = result_data['buildName']
        app_version = result_data['buildVersion'] + '.' + result_data['buildVersionNo']
        app_shortcut_url = result_data['buildShortcutUrl']
        # app_picture_url = result_data['buildIcon']

        headers = {'Content-Type': 'application/json'}

        # link_content = {}
        # link_content['title'] = '%s has been Ready!' % (app_name)
        # link_content['text'] = '%s:\nMay the force be with you.' % (app_version)
        # link_content['messageUrl'] = PGYER_URL_PRE + app_shortcut_url
        # link_content['picUrl'] = PGYER_ICON_URL_PRE + app_picture_url

        # data = {}
        # data['msgtype'] = 'link'
        # data['link'] = link_content

        content = {}
        content['text'] = '%s v%s has been ready!\n%s\nMay the force be with you.' % (app_name, app_version, PGYER_URL_PRE + app_shortcut_url)

        data = {}
        data['msg_type'] = 'text'
        data['content'] = content

        print('Upload Dev APK to pgyer:')
        ret = http.post(FEISHU_WEB_HOOK_DEV_URL, data=json.dumps(data), headers=headers)
        print(ret)

        if self.env == 'pre' or self.env == 'prod':
            print('Upload APK to pgyer, env=%s:' % self.env)
            ret = http.post(FEISHU_WEB_HOOK_URL, data=json.dumps(data), headers=headers)
            print(ret)

